import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../remote/request.dart';
import '../../../services/api_service.dart';

class NoticeReportLogic extends GetxController {
  final apiService = Get.find<ApiService>();

  static const String actionProgress = 'progress';
  static const String actionReturn = 'return';

  final List<String> returnContents = [
    '我还有更重要的案件处理',
    '想处理其他案源',
    '该案源比较复杂，处理困难',
    '该案源联系人无法联系',
    '当事人无付费意愿',
    '当事人需要全风险代理',
    '其他原因'
  ];

  final Map<String, String> actionTitleMap = {
    actionProgress: '案源进展',
    actionReturn: '案源退回',
  };

  var reportAction = ''.obs;
  var reportContent = ''.obs;
  var isSubmitting = false.obs;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final TextEditingController progressController = TextEditingController();
  final TextEditingController returnController = TextEditingController();

  late NoticeReportParams params;

  @override
  void onInit() {
    super.onInit();

    params = Get.arguments;
    reportAction.value = params.reportAction ?? '';
  }

  void onSelectContent(String content) {
    reportContent.value = content;
  }

  void onSubmit() {
    if (formKey.currentState!.validate()) {
      formKey.currentState!.save();

      if (reportAction.value == actionProgress) {
        reportContent.value = progressController.text;
        _noticeAction();
      } else if (reportAction.value == actionReturn) {
        if (returnController.text.isNotEmpty) {
          reportContent.value = returnController.text;
        }
        _noticeAction();
      }
    }
  }

  void _noticeAction() => request(() async {
        var res = await apiService.noticeAction(
          reportAction.value,
          id: params.noticeId,
          remark: reportContent.value,
        );
        if (res != null) {
          EasyLoading.showSuccess('提交成功！');
          Get.back(result: true);
        } else {
          EasyLoading.showError('提交失败，请稍后重试！');
        }
      }, showLoading: true, message: '正在提交...');
}

class NoticeReportParams {
  int? noticeId;
  String? reportAction;

  NoticeReportParams({
    this.noticeId,
    this.reportAction,
  });
}
